package com.neu.wms.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.wms.dto.ItemDto;
import com.neu.wms.entity.WmsItem;
import com.neu.wms.service.ItemService;
import com.neu.wms.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/material")
public class ItemController {
    @Autowired
    private ItemService itemService;
    @PostMapping("queryAll")
    public ResultVo selectAll(@RequestBody ItemDto itemDto)
    {
        PageHelper.startPage(itemDto.getCurrentPage(),itemDto.getPageSize(),true);
        List<WmsItem> item = itemService.selectAll(itemDto);
        PageInfo<WmsItem> pageInfo = new PageInfo<>(item);
        return ResultVo.ok().data("list",item).data("total",pageInfo.getTotal());
    }


    @GetMapping("{id}")
    public ResultVo queryById(@PathVariable("id") Long id){
        WmsItem wmsItem = itemService.selectByPrimaryKey(id);
        return ResultVo.ok().data("item",wmsItem);
    }

    @PostMapping("")
    public ResultVo add(@RequestBody  WmsItem item)
    {
        System.out.println("1111");
        int i = itemService.insert(item);
        if (i>0) {
            return ResultVo.ok("添加成功");
        }else {
            return ResultVo.error("添加失败");
        }
    }

    @PutMapping("")
    public ResultVo update(@RequestBody  WmsItem wmsItem)
    {
        int i = itemService.update(wmsItem);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @PutMapping("/quit")
    public ResultVo updateDelFlag(@RequestBody ItemDto itemDto)
    {
        if(itemDto.getDelFlag()==null)
            throw new IllegalArgumentException("Argument cannot be null or empty");
        int i = itemService.updateDelFlag(itemDto);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }

    @DeleteMapping("{id}")
    public ResultVo delete(@PathVariable("id")Long id)
    {
        int i = itemService.delete(id);
        if (i>0) {
            return ResultVo.ok("删除成功");
        }else {
            return ResultVo.error("删除失败");
        }
    }
}
